package cn.zy.gvs.v1.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.zy.gvs.v1.core.common.base.BaseController;
import cn.zy.gvs.v1.core.common.domain.AjaxResult;
import cn.zy.gvs.v1.core.common.domain.ResultTable;
import cn.zy.gvs.v1.model.GoViewProject;
import cn.zy.gvs.v1.model.GoViewProjectData;
import cn.zy.gvs.v1.model.vo.SysFileVo;
import cn.zy.gvs.v1.service.IGoViewProjectDataService;
import cn.zy.gvs.v1.service.IGoViewProjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("${gvs.apiPrefix}/project")
public class GoViewProjectController extends BaseController {

    @Resource
    private IGoViewProjectService goViewProjectService;

    @Resource
    private IGoViewProjectDataService goViewProjectDataService;

    @Resource
    private FileStorageService fileStorageService;

    /**
     * 大屏列表查询
     *
     * @param pageNum 页码
     * @param limit   分页大小
     */
    @GetMapping("/list")
    public ResultTable list(@RequestParam("page") Long pageNum, @RequestParam("limit") Long limit) {
        Page<GoViewProject> page = new Page<>(pageNum, limit);
        IPage<GoViewProject> iPages = goViewProjectService.page(page, new LambdaQueryWrapper<>());
        // 构建相应数据
        ResultTable resultTable = new ResultTable();
        resultTable.setData(iPages.getRecords());
        resultTable.setCode(200);
        resultTable.setCount(iPages.getTotal());
        return resultTable;
    }


    /**
     * 新增保存
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody GoViewProject goViewProject) {
        return AjaxResult.success(goViewProjectService.saveProject(goViewProject));
    }


    /**
     * 项目表删除
     *
     * @param ids
     */
    @DeleteMapping("/delete")
    public AjaxResult remove(String ids) {
        List<String> list = StrUtil.split(ids, ",");
        return toAjax(goViewProjectService.removeByIds(list));
    }

    /**
     * 编辑
     */
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody GoViewProject goViewProject) {
        return toAjax(goViewProjectService.updateById(goViewProject));
    }

    /**
     * 重命名
     */
    @PostMapping("/rename")
    public AjaxResult rename(@RequestBody GoViewProject goViewProject) {

        LambdaUpdateWrapper<GoViewProject> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GoViewProject::getId, goViewProject.getId());
        updateWrapper.set(GoViewProject::getProjectName, goViewProject.getProjectName());
        return toAjax(goViewProjectService.update(updateWrapper));
    }

    /**
     * 发布/取消项目状态
     */
    @PutMapping("/publish")
    public AjaxResult updateVisible(@RequestBody GoViewProject goViewProject) {

        if (!Arrays.asList(1, -1).contains(goViewProject.getState())) {
            return error("警告非法字段");
        }
        LambdaUpdateWrapper<GoViewProject> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GoViewProject::getId, goViewProject.getId());
        updateWrapper.set(GoViewProject::getState, goViewProject.getState());
        return toAjax(goViewProjectService.update(updateWrapper));
    }


    @GetMapping("/getData")
    public AjaxResult getData(String projectId) {
        return AjaxResult.success(goViewProjectService.getProjectById(projectId));
    }


    @PostMapping("/save/data")
    public AjaxResult saveData(GoViewProjectData data) {
        GoViewProject goViewProject = goViewProjectService.getById(data.getProjectId());
        if (goViewProject == null) {
            return error("没有该项目ID");
        }
        return toAjax(goViewProjectDataService.insertOrUpdate(data));
    }

    /**
     * 上传文件
     * 这里针对本地上传和oss上传需要返回两种
     * p1: 本地上传时需返回附带访问域名的url全路径
     * p2: oss上传时仅需返回不带域名的fileName, 然后在</getOssInfo>接口中返回oss域名
     *
     * @param object     文件流对象 [fileName: ${projectId}_index_preview.png || ${projectId}_index_background.png]
     * @return 文件信息
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestBody MultipartFile object) throws IOException {

        String format = DateUtil.format(new Date(), "yyyy-MM-dd");
        FileInfo fileInfo = fileStorageService.of(object)
                // 增加日期目录
                .setPath(format + "/")
                // 文件用途
                .putAttr("fileUse", "0")
                // 开始上传
                .upload();
        SysFileVo fileVo = new SysFileVo();
        fileVo.setFileName(fileInfo.getFilename());
        fileVo.setFileUrl(fileInfo.getUrl());
        return AjaxResult.success(fileVo);
    }
}
